package de.tabuma.pm.project.controller;

import de.tabuma.pm.projectnavigation.controller.list;
import de.tabuma.pm.projectnavigation.entity.*;
import de.tabuma.pm.projectnavigation.logik.*;
import de.tabuma.pm.project.entity.Project;
import de.tabuma.pm.project.logik.ProjectFacade;
import de.tabuma.lib.tabumacontroller;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.*;
import de.tabuma.customer.logik.*;
import de.tabuma.customer.entity.*;
import de.tabuma.pm.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class controller extends tabumacontroller {

    private static final Logger log = Logger.getLogger(controller.class.getName());
    public controller(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }
    
    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        String id = request.getParameter(constants.projectId);
        int idInt = Integer.valueOf(id).intValue();
        Project project = ProjectFacade.get(idInt);

        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        
        if(request.getParameter(constants.paramProjectUpdate)!=null) {
            String projectName = request.getParameter(constants.paramProjectName);
            String projectDescription = request.getParameter(constants.paramProjectDescription);
            String projectCustomerId = request.getParameter(constants.paramProjectCustomerId);
            int projectCustomerIdInt = Integer.valueOf(projectCustomerId).intValue();

            String projectStartpage = request.getParameter(constants.paramProjectStartpage);
            int projectStartpageInt = Integer.valueOf(projectStartpage).intValue();

            project.setCustomerId(projectCustomerIdInt);
            project.setDescription(projectDescription);
            project.setName(projectName);
            project.setStartPageId(new Integer(projectStartpageInt));
            ProjectFacade.update(project, user.getEmail());
        }

        int projectActionParam = constants.PROJECT_ACTION_PARAM_NAVIGATION;
        if(request.getParameter(constants.PROJECT_ACTION_PARAM)!=null) {
            projectActionParam = Integer.valueOf(request.getParameter(constants.PROJECT_ACTION_PARAM));
            request.setAttribute(constants.PROJECT_ACTION_PARAM, ""+projectActionParam);
        }

        request.setAttribute(constants.paramAction, "" + constants.ACTION_OVERVIEW);
        request.setAttribute(constants.paramProject, project);
        List<Customer> cList = CustomerFacade.getCustomer(user.getEmail());
        request.setAttribute(constants.customerList, cList);
        List<ProjectNavigation> pnList = ProjectNavigationFacade.getList(idInt);
        request.setAttribute(constants.paramProjectStartpageList, pnList);

        Hashtable hash = (Hashtable) this.getParameter().clone();
        hash.put(constants.projectId, id);
        hash.put(constants.paramAction, ""+constants.ACTION_OVERVIEW);
        hash.put(constants.PROJECT_ACTION_PARAM, projectActionParam);

        de.tabuma.pm.externalproject.controller.controller epc = new de.tabuma.pm.externalproject.controller.controller(this.getServlet(), hash);
        epc.navigate(context, request, response, out);

        list lpnc = new list(this.getServlet(), hash);
        lpnc.navigate(context, request, response, out);
        
    }
}